<!DOCTYPE html>
<html>
<head>
    <#import "../common/common.macro.ftl" as netCommon>
    <title>${title!"艺术上海"}</title>
    <meta charset="utf-8" />
    <meta name="keywords" content="${keywords?if_exists}" />
    <meta name="description" content="${description?if_exists}" />
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <link href="/static/css/bootstrap.mini.css" rel="stylesheet" />
    <link href="/static/css/style.css" rel="stylesheet" />
    <link href="/static/css/common/index.css" rel="stylesheet" />

    <link href="/static/css/footer/index.css" rel="stylesheet" />
    <link href="/static/css/font/font-awesome.min.css" rel="stylesheet" />
    <link href="/static/css/font/font-awesome.min.css" rel="stylesheet" />
    <link href="/static/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <style>
        .container-p{
            width: 1200px;
        }
        .padding-y{
            margin: 20px;
        }
        .form-group label{
            margin: 10px 0px;
        }

        .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            padding: 0.75rem;
            vertical-align: middle;
            border-top: 1px solid #dee2e6;
        }

        .img-sm{
            max-width: 90px;
            margin-right: 10px;
        }
        .itemside{
            display: flex;
            align-items: center;
            justify-content: flex-start;
        }

    </style>
</head>
<body>
<!-- header -->
<@netCommon.commonHeader "works" menu i18n/>

<section class="section-pagetop bg" style="margin-top: 110px">
    <div class="container container-p">
        <h2 class="title-page">我的订单</h2>
    </div> <!-- container //  -->
</section>

<section class="section-content padding-y ">
    <div class="container container-p">

        <!-- =========================  COMPONENT MYORDER 1 ========================= -->
        <div class="row">
            <aside class="col-md-3">
                <!--   SIDEBAR   -->
                <ul class="list-group">
                    <a class="list-group-item active" href="/user/order"> 我的订单 </a>
                    <a class="list-group-item " href="/user/setting"> 个人设置 </a>
                    <a class="list-group-item " href="/user/address"> 我的收货地址 </a>
                    <a class="list-group-item" href="/user/history"> 足迹 </a>
                </ul>


            </aside>
            <main class="col-md-9">

                <article class="card">
                    <div class="card-body">
                        <h4 class="card-title mb-4" style="margin-bottom: 20px">我的订单</h4>
                        <table class="table table-borderless table-shopping-cart">
                            <thead class="text-muted">
                            <tr class="text-uppercase">
                                <th scope="col">产品</th>
                                <th scope="col" width="120">数量</th>
                                <th scope="col" width="120">价格</th>
                                <th scope="col" width="120">状态</th>
                                <th scope="col" class="text-center" width="200">操作</th>
                            </tr>
                            </thead>
                            <tbody id="address-list">


                            </tbody>
                        </table>
                    </div> <!-- card-body .// -->

                </article> <!-- order-group.// -->
            </main>
        </div> <!-- row.// -->
    </div>
</section>
<!-- footer -->
<@netCommon.commonFooter footer friendLink copyright i18n/>

<!-- script -->
<@netCommon.commonScript />


<script>
    var list ;//收货地址

    $(function () {
        queryOrderList();

    })


    function queryOrderList() {

        var param = {
            page: 1,
            rows: 40,

        }

        $.ajax({
            type: "POST",
            url: baseUrl+"api/shop-center/app/mall/order/query",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                'lbv': '1.0',
                'tenant' : 'PTC',
                'token':  getToken(),
            },
            data: JSON.stringify(param),
            success: function (res) {
                if(res && res.code && res.code=='0' ){
                    if(res.data && res.data.records){

                        list = res.data.records;

                        for(var i=0; i<list.length; i++){
                            // var html = '  <p>'+list[i].linkManName +' '+list[i].linkManMobile+'</p> <p>'+list[i].addr+'</p>'
                            //var html = '<tr  class="addr-'+list[i].id+'" data-id="+i+"><td><a href="#"  class="btn  btn-primary btn-sel"  data-index="'+i+'" data-id="'+list[i].id+'">选择</a></td><td>'+list[i].linkManName +'</td> <td>'+list[i].linkManMobile+'</td> <td>'+list[i].addr+'</td><td><a href="#" class="btn btn-light btn-edit" data-id="'+list[i].id+'"> 编辑 </a> <a href="#" class="btn btn-light  btn-del" data-id="'+list[i].id+'"> 删除 </a></td></tr> '

                            var items = list[i].items
                            var items_html = '';
                            var quantity = 0;
                            for(var j=0; j<items.length; j++){
                                var html = '<figure class="itemside"><div class="aside"><img src="'+items[j].thumbnail+'" style="object-fit: cover; width: 140px; height: 140px; margin: 20px"></div><figcaption class="info"><a href="/user/orderDetail?sn='+list[i].sn+'" class="title text-dark">'+items[j].name+'</a></figcaption></figure>'
                                items_html = items_html + html;
                                quantity = quantity + items[j].quantity;
                            }
                            // OSE_Pay("10", "待付款"),
                            //     OSE_Check("20", "待审核)"),
                            //     OSE_Send("30", "待发货(待使用)"),
                            //     OSE_Send_Part("40", "部分发货"),
                            //     OSE_Receiving("50", "待收货"),
                            //     OSE_Complete("60", "交易完成(已使用)"),
                            //     OSE_Close("70", "交易关闭"),
                                var orderTypeTxt =''
                            var btnHtml =''
                            if(list[i].status=='10'){
                                orderTypeTxt = '待付款'
                                btnHtml = '<a href="#" class="btn btn-light btn-pay" data-index="'+i+'" > 支付</a> <a href="#" class="btn btn-light btn-cancel" data-index="'+i+'" > 取消订单</a>'
                            }
                            else if(list[i].status=='20'){
                                orderTypeTxt = '等待审核'
                            }
                            else if(list[i].status=='30'){
                                orderTypeTxt = '等待发货'
                                //btnHtml = '<a href="#" class="btn btn-light btn-pay" data-index="'+i+'" > 提醒发货</a>'
                            }
                            else if(list[i].status=='40'){
                                orderTypeTxt = '部分发货'
                            }
                            else if(list[i].status=='50'){
                                orderTypeTxt = '等待收货'
                                btnHtml = '<a href="#" class="btn btn-light btn-confirm" data-index="'+i+'" >  确认收货</a>'
                            }
                            else if(list[i].status=='60'){
                                orderTypeTxt = '交易完成'
                            }

                            else if(list[i].status=='70'){
                                orderTypeTxt = '交易关闭'
                            }

                            var html = '<tr class="order-list" data-index="'+i+'"><td>'+items_html+'</td><td>  '+quantity+'件</td><td><div class="price-wrap"><var class="price">¥'+list[i].fund+'</var><small class="text-muted"> ¥'+list[i].freight+' 运费</small></div></td><td>   '+orderTypeTxt+'</td><td class="text-center">'+btnHtml+'</td></tr>'
                            $('#address-list').append(html)

                        }

                        $('.btn-pay').click(function () {
                            var index = $(this).attr('data-index');
                            var sn = list[index].sn;
                            var price = list[index].fund;
                            window.location.href = '/order/pay?sn='+sn+'&money='+price;

                        })

                        $('.btn-confirm').click(function () {
                            var index = $(this).attr('data-index');
                            var sn = list[index].sn;
                            var r=confirm("是否确认收货？此操作不可撤回");
                            if (r==true) {
                                $.ajax({
                                    type: "GET",
                                    url: baseUrl+"api/shop-center/app/mall/order/complete?sn="+sn,
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    headers: {
                                        'lbv': '1.0',
                                        'tenant' : 'PTC',
                                        'token':  getToken(),
                                    },
                                    success: function (res) {
                                        if(res && res.code && res.code=='0' ){
                                            window.location.reload();
                                        }
                                        else{
                                        alert(res.msg);
                                        if(res.code=='-3'){
                                            relogin(window.location.href);
                                        }
                        
                    }
                                    },
                                    error: function (res) {

                                    }
                                });
                            }
                        })
                        // $('.order-list').click(function () {
                        //     var index = $(this).attr('data-index');
                        //     var sn = list[index].sn;
                        //     window.location.href='/user/orderDetail?sn='+sn
                        // })
                       $('.btn-cancel').click(function () {
                           var r=confirm("是否要取消订单");
                           if (r==true) {
                               var index = $(this).attr('data-index');
                               var sn = list[index].sn;
                               $.ajax({
                                   type: "GET",
                                   url: baseUrl+"api/shop-center/app/mall/order/close?sn="+sn,
                                   contentType: "application/json; charset=utf-8",
                                   dataType: "json",
                                   headers: {
                                       'lbv': '1.0',
                                       'tenant' : 'PTC',
                                       'token':  getToken(),
                                   },
                                   success: function (res) {
                                       if(res && res.code && res.code=='0' ){
                                           window.location.reload();
                                       }
                                       else{
                        alert(res.msg);
                        if(res.code=='-3'){
                            relogin(window.location.href);
                        }
                        
                    }
                                   },
                                   error: function (res) {

                                   }
                               });
                           }


                       })

                    }

                    $(".btn-sel").click(function () {
                        var selid= $(this).attr('data-id');
                        var index =  $(this).attr('data-index');
                        var currAddr = list[index];
                        localStorage.setItem('currAddr',JSON.stringify(currAddr))
                        window.history.back(-1);
                    })
                    $(".btn-edit").click(function () {
                        var selid= $(this).attr('data-id')
                        window.location.href='/user/addressDetail?id='+selid;
                    })
                    $(".btn-del").click(function () {
                        var selid= $(this).attr('data-id')
                        var r=confirm("是否要删除");
                        if (r==true){
                            $.ajax({
                                type: "GET",
                                url: baseUrl+"api/user-center/app/cust/addr/delete?id="+selid,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                headers: {
                                    'lbv': '1.0',
                                    'tenant' : 'PTC',
                                    'token':  getToken(),
                                },
                                success: function (res) {
                                    if(res && res.code && res.code=='0' ){
                                        alert('删除成功')
                                        $(".addr-"+selid).remove();
                                    }
                                    else{
                        alert(res.msg);
                        if(res.code=='-3'){
                            relogin(window.location.href);
                        }
                        
                    }
                                },
                                error: function (res) {

                                }
                            });
                        }

                    })
                }else{
                        alert(res.msg);
                        if(res.code=='-3'){
                            relogin(window.location.href);
                        }
                        
                    }
            },
            error: function (res) {

            }
        });
    }

</script>



</body>
</html>
